package com.m20studio.mop.xml;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.safety.Whitelist;
import org.jsoup.select.Elements;

import android.net.ParseException;
import android.util.Log;


public class XMLHandler{

	private List<XMLFeed> xmllists;
	private XMLFeed feed;
	public String fpURL;

	public XMLHandler(){
		xmllists = new ArrayList<XMLFeed>();
	}

	public List<XMLFeed> getListFeed(){
		return xmllists;
	}
	
	//最新帖子列表
	public void handlerClass(String html){

		
		Document doc =Jsoup.parse(html);
		Elements url = doc.select(".tableC .t_w1 a");
		Elements title = doc.select(".odd .t_w1 a");
		Elements hf = doc.select(".odd .t_w2");
		Elements time = doc.select(".odd .t_w3");
		

		for (int i = 0; i < title.size(); i++){
			feed = new XMLFeed();
			//判断日期格式并转换
			
			List list = Arrays.asList(url.get(i).attr("href").split("/"));
			String time_str = null;
			try {
				time_str = getDateFmtStrFromEn(time.get(i).text(), false);
				Log.e("m20", time.get(i).text());
			} catch (Exception e) {
				e.printStackTrace();
			}
			list.set(2,time_str);
			//判断结束
			feed.setURL("http://dzh.mop.com"+list.toString().replaceAll(",", "/").replaceAll(" ", "").replace("[", "").replace("]", ""));
			feed.setTitle(title.get(i).text());
			feed.setHf(hf.get(i).text());
			feed.setTime(time_str);
			xmllists.add(feed);
		}
		
						
	}
	
	//除最新贴之外其他帖子列表
	public void handlerClassA(String html){

		
		Document doc =Jsoup.parse(html);
		Elements url = doc.select(".tableC .t_w1 a");
		Elements title = doc.select(".odd .t_w1 a");
		Elements hf = doc.select(".odd .t_w2");
		Elements time = doc.select(".odd .t_w3");

		for (int i = 0; i < title.size(); i++){
			feed = new XMLFeed();					
			feed.setURL("http://dzh.mop.com"+url.get(i).attr("href"));
			feed.setTitle(title.get(i).text());
			feed.setHf(hf.get(i).text());
			feed.setTime(time.get(i).text());
			xmllists.add(feed);
		}
		
						
	}
	
	
	public void handlerViewTz(String html, int screenWidth, int page){
		
		long startSc = System.currentTimeMillis();
		String content_str;
		Document doc =Jsoup.parse(html);
		//Log.e("m20","耗时1："+(System.currentTimeMillis()-startSc));
		//帖子标题
		Elements title = doc.select("#title");
		//帖子回复数
		Elements info = doc.select("div.llhfP");
		info.select("a").remove();
		//楼主正文
		Elements content_lz = doc.select("#body");
		content_lz.select("a").remove();
		String content_lz_str = Jsoup.clean(content_lz.html(), Whitelist.basicWithImages()).replaceAll("\"", "'");
		//楼主姓名
		Elements lz_name = doc.select("#lzxx_fun .inner a");
		//楼主时间tzrq
		Elements lz_time = doc.select(".tzrq");
		//楼主全部内容组合
		String time = null;
		
		//Log.e("m20","耗时2："+(System.currentTimeMillis()-startSc));
		//try {
		//	time = getDateFmtStrFromCST(lz_time.text(), true);
		//} catch (Exception e1) {
		//	e1.printStackTrace();
		//}
		time = lz_time.text();
		if (page==0){
			content_str = addCSS(screenWidth)+
				"<div class=title>"+title.text()+"</div>"+
				"<div class=info>"+info.text()+"</div>"+				
				"<div class=content>"+content_lz_str+"</div>"+		
				"<div class=name>楼主:"+lz_name.text()+"</div><div class=time>时间:"+time+"</div>";
		}else{
			content_str = addCSS(screenWidth)+
				"<div class=title>"+title.text()+"</div>"+
				"<div class=info>"+info.text()+"</div>";
		}		
		//Log.e("m20","耗时3："+(System.currentTimeMillis()-startSc));
		
		//获取尾页
		Elements lastPage = doc.select(".endgray");
		Elements onPage = doc.select("a.on");
		String te = null;
		int pageCount;
		
		if (lastPage.isEmpty()){
			pageCount = 0;			
		}else{			
			te = lastPage.attr("href");
			if (te.equals("")){
				pageCount = Integer.parseInt(onPage.get(0).text())-1;
			}else{
				pageCount = Integer.parseInt(te.split("/")[3]);
			}
		}		
				
		//回复
		Elements hf = doc.select(".tzhfP");
		if (hf.size()<1){
			feed.setContent("<div class=content>"+content_str+
					"</div></body></html>");
		}else {
			String content_hf_str = Jsoup.clean(hf.html(), Whitelist.basicWithImages()).replaceAll("\"", "'");		
			content_str += "<div class=content>"+content_hf_str;		
			feed = new XMLFeed();
			feed.setPageCount(pageCount);
			feed.setContent(content_str+
					"</div></body></html>");
		}	
		
		xmllists.add(feed);
		
		//Log.e("m20","耗时4："+(System.currentTimeMillis()-startSc));
		
						
	}
	
	//将EN时间格式转成格式
	public static String getDateFmtStrFromEn(String cststr, boolean type) throws Exception {  
	    if (null == cststr) {  
	        return null;  
	    }  
	    String str = null;  
	    SimpleDateFormat sdfy;
	    if(type == false){
	    	sdfy = new SimpleDateFormat("yyyyMMdd"); 
	    }else{
	    	sdfy = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
	    }
	    SimpleDateFormat sdf = new SimpleDateFormat(  
	            "MMM dd',' yyyy", Locale.US);  
	    try {  
	        str = sdfy.format(sdf.parse(cststr.trim()));  
	    } catch (ParseException e) {  
	        e.printStackTrace();  
	        return null;  
	    }  
	    return str;  
	}
	//将CST时间格式转成格式
	public static String getDateFmtStrFromCST(String cststr, boolean type) throws Exception {  
	    if (null == cststr) {  
	        return null;  
	    }  
	    String str = null;  
	    SimpleDateFormat sdfy;
	    if(type == false){
	    	sdfy = new SimpleDateFormat("yyyyMMdd"); 
	    }else{
	    	sdfy = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
	    }
	    SimpleDateFormat sdf = new SimpleDateFormat(  
	            "MMM dd',' yyyy", Locale.US);  
	    try {  
	        str = sdfy.format(sdf.parse(cststr.trim()));  
	    } catch (ParseException e) {  
	        e.printStackTrace();  
	        return null;  
	    }  
	    return str;  
	}  
	
	

	//WebView CSS样式
	public String addCSS(int screenWidth){		
		
		//Log.e("mop", screenWidth+"");
		String css="<html><link rel=stylesheet href='file:///android_asset/"+screenWidth+".css' type='text/css'><body>";
		return css;
	}
		
}

